@using Lombiq.HelpfulExtensions.Extensions.Widgets.Constants
@using Lombiq.HelpfulExtensions.Extensions.Widgets.Models
@using Lombiq.HelpfulLibraries.Common.Utilities;
@using Microsoft.AspNetCore.Http.Extensions
@using OrchardCore.Navigation
<style asp-name="font-awesome"></style>

@{
    var viewModel = new MenuWidgetViewModel(Model);

    const string blockName = "menuWidget";
    const string contentElementName = blockName + "__content";
    const string togglerElementName = blockName + "__toggler";
    const string dropdownElementName = blockName + "__dropdown";

    var contentIdName = StringHelper.CreateInvariant($"{contentElementName}_{Guid.NewGuid()}");

    var baseUri = new Uri(Context.Request.GetDisplayUrl());

    var menuItems = viewModel
        .MenuItems
        .OrderBy(item => double.TryParse(item.Position, out var number) ? number : double.MaxValue)
        .ThenBy(item => item.Position);
}

@if (!viewModel.NoWrapper)
{
    @:<nav class="navbar navbar-expand-md @viewModel.HtmlClasses">
}

<div class="container-fluid @(viewModel.NoWrapper ? viewModel.HtmlClasses : string.Empty)">
    <button class="navbar-toggler @togglerElementName"
            type="button"
            data-bs-toggle="collapse"
            data-bs-target="#@contentIdName"
            aria-controls="@contentIdName"
            aria-expanded="false"
            aria-label="@T["Toggle Navigation"]">
        <span class="navbar-toggler-icon fas fa-bars"></span>
    </button>
    <div id="@contentIdName" class="collapse navbar-collapse @contentElementName">
        <ul class="navbar-nav me-auto mb-lg-0">
            @foreach (var menuItem in menuItems)
            {
                var uri = new Uri(baseUri, menuItem.Href);
                var isActive = !menuItem.IsDivider() && baseUri.AbsolutePath.EqualsOrdinalIgnoreCase(uri.AbsolutePath);
                var classes = $"nav-link {(isActive ? "active" : string.Empty)} {menuItem.Classes.Join()}"
                    .Split(" ", StringSplitOptions.RemoveEmptyEntries);

                @if (menuItem.Items.Any())
                {
                    var subMenuItems = menuItem
                        .Items
                        .OrderBy(item => double.TryParse(item.Position, out var number) ? number : double.MaxValue)
                        .ThenBy(item => item.Position);

                    <li class="nav-item dropdown">
                        <shape type="MenuWidgetItem"
                               prop-Id="@StringHelper.CreateInvariant($"{dropdownElementName}_{Guid.NewGuid()}")"
                               prop-ViewModel="@viewModel"
                               prop-Item="@menuItem"
                               prop-SubMenuItems="@subMenuItems">
                            <metadata>
                                <add-alternate name="@($"{ShapeTypes.MenuWidgetItem}__{MenuWidgetItemType.Parent}")" />

                                @foreach (var className in classes)
                                {
                                    <add-class name="@className" />
                                }
                            </metadata>
                        </shape>
                    </li>
                }
                else
                {
                    var alternateName = menuItem.GetMenuWidgetItemShapeName(baseUri);
                    <li class="nav-item menuWidget__topLevel @(isActive ? "nav-item-link-active" : string.Empty)">
                        <shape type="MenuWidgetItem" prop-ViewModel="@viewModel" prop-Item="@menuItem">
                            <metadata>
                                <add-alternate name="@alternateName"/>

                                @if (!string.IsNullOrWhiteSpace(menuItem.Id))
                                {
                                    <add-alternate name="@($"{alternateName}_{menuItem.Id}")"/>
                                }

                                @foreach (var className in classes)
                                {
                                    @if (!string.IsNullOrWhiteSpace(className))
                                    {
                                        @if (className != "nav-link")
                                        {
                                            <add-alternate name="@($"{alternateName}_{className}")"/>
                                        }

                                        <add-class name="@className" />
                                    }
                                }
                            </metadata>
                        </shape>
                    </li>
                }
            }
        </ul>
    </div>
</div>

@if (!viewModel.NoWrapper)
{
    @:</nav>
}
